package com.threadExample;

import java.util.concurrent.*;

/**
 * Created by yijy on 2018/4/11.
 */
public class FutureAndThread{

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        FutureAndThread futureAndThread = new FutureAndThread();
        Future<?> future = executorService.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                return futureAndThread.toTell();
            }
        });
        future.cancel(true);
        System.out.println(future.isCancelled());
        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }finally {
            executorService.shutdown();
        }
    }

    public int toTell(){
        int count=0;
        while (count<20){
            System.out.println(Thread.currentThread().getName()+":"+count);
            count++;
//            try {
//                Thread.sleep(100);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
        return count;
    }


}
